<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 从键盘输入字符串, 当满足暗号时提示 -->
    <script>
        const secretCode = 'chl';
        const pressed = [];
        window.addEventListener('keyup', (e) => {
            pressed.push(e.key);
            const str = pressed.join('');
            console.log(str);
            const reg = new RegExp(secretCode);
            if (reg.test(str)) { 
                alert('输入暗号成功!');
                pressed.length = 0; // 成功后清空数组
            }
        });
    </script>
</body>

</html>